package com.yuanshi.food.common.exception;

import cn.hutool.core.text.CharSequenceUtil;
import com.yuanshi.food.common.enums.ResponseCodeEnum;
import lombok.Getter;

import java.io.Serial;

/**
 * 应用层例外
 *
 * @author wcy
 * @date 2024/12/03
 */
@Getter
public class AppException extends RuntimeException {

    @Serial
    private static final long serialVersionUID = 7722109773917201890L;
    private final int code;
    private final String info;

    public AppException(int code) {
        this.code = code;
        this.info = CharSequenceUtil.EMPTY;
    }

    public AppException(int code, Throwable cause) {
        super.initCause(cause);
        this.code = code;
        this.info = CharSequenceUtil.EMPTY;
    }

    public AppException(int code, String message) {
        super(message);
        this.code = code;
        this.info = message;
    }

    public AppException(int code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.info = message;
    }

    public AppException(ResponseCodeEnum responseCodeEnum) {
        this.code = responseCodeEnum.getCode();
        this.info = responseCodeEnum.getInfo();
    }

    public AppException(ResponseCodeEnum responseCodeEnum, Throwable cause) {
        super(responseCodeEnum.getInfo(), cause);
        this.code = responseCodeEnum.getCode();
        this.info = responseCodeEnum.getInfo();
    }

    @Override
    public String toString() {
        return "MyException{" +
                "status=" + code +
                ", info='" + info + '\'' +
                '}';
    }
}
